<?php
include_once 'ManageUsersPresenter.php';
include_once 'AbstractView.php';
include_once 'InsultBuilder.php';
class ManageUsersView extends AbstractView {
	private $presenter;
	public function __construct() {
		parent::__construct ();
		$this->presenter = new ManageUsersPresenter ( $this, parent::getUserRepository (), parent::getActiveUser () );
	}
	public function updateSucceed() {
		$builder = new InsultBuilder ();
		$builder->setMessage ( "See how ZOG is ruling the world!" )->setTitle ( "Hell yeah!" )->setPicture ( "manageusers_succeed.jpg" )->setSource ( "manageusers.php" );
		parent::redirect ( $builder->build () );
	}
	public function updateError($reason) {
		$builder = new InsultBuilder ();
		$builder->setMessage ( $reason )->setTitle ( "Hell no!" )->setPicture ( "userprofile_failed.jpg" )->setSource ( "manageusers.php" );
		parent::redirect ( $builder->build () );
	}
	public function render() {
		if (! parent::invokedByAdmin ()) {
			parent::block ();
			return;
		}
		
		if (isset ( $_POST ["onClicked"] )) {
			
			$toDelete = array ();
			foreach ( $_POST ["killtheprick"] as $postParameter ) {
				foreach ( $postParameter as $user => $value ) {
					$toDelete [] = $user;
				}
			}
			
			$toMakeAdmin = array ();
			foreach ( $_POST ["permission"] as $postParameter ) {
				foreach ( $postParameter as $user => $value ) {
					$toMakeAdmin [] = $user;
				}
			}
			
			$this->presenter->applyChanges ( $toDelete, $toMakeAdmin );
			
			return;
		}
		
		require 'manageUsr.phtml';
	}
	public function renderUserList() {
		$users = $this->presenter->getUserList ();
		
		foreach ( $users as $user ) {
			echo '<tr>';
			echo '<td>' . htmlspecialchars ( $user->getUserName () ) . '</td>';
			echo '<td>' . htmlspecialchars ( $user->getName () ) . '</td>';
			echo '<td>' . htmlspecialchars ( $user->getEmail () ) . '</td>';
			if ($user->getPrivileges () == User::ADMIN_PRIVILEGES) {
				echo '<td><input type="checkbox" name="permission[][' . htmlspecialchars ( $user->getUserName () ) . ']"  checked></td>';
			}
			if ($user->getPrivileges () == User::USER_PRIVILEGES) {
				echo '<td><input type="checkbox" name="permission[][' . htmlspecialchars ( $user->getUserName () ) . ']"  ></td>';
			}
			echo '<td><input type="checkbox" name="killtheprick[][' . htmlspecialchars ( $user->getUserName () ) . ']" "></td>';
			
			echo '</tr>';
		}
	}
}

$view = new ManageUsersView ();
$view->render ();
